package gstyle.compressor;

import gstyle.Style;
import gstyle.compressor.CssCompressor;

import java.util.Map;

public class BackgroundCompressor implements CssCompressor {
    public Map compress(Map attributes) {
        String background = (String) attributes.remove(Style.BACKGROUND);
        String color = (String) attributes.remove(Style.BACKGROUND_COLOR);
        String image = (String) attributes.remove(Style.BACKGROUND_IMAGE);
        String position = (String) attributes.remove(Style.BACKGROUND_POSITION);
        String repeat = (String) attributes.remove(Style.BACKGROUND_REPEAT);
        String compressedBackground = background(background, color, image, position, repeat);
        if (!"".equals(compressedBackground)) {
            attributes.put("background", compressedBackground);
        }
        return attributes;
    }

    public String background(String background, String backgroundColor, String backgroundImage, String backgroundPosition, String backgroundRepeat) {
        String fullBackground = blankIfNull(background)
                + blankIfNull(backgroundColor)
                + blankIfNull(backgroundImage)
                + blankIfNull(backgroundPosition)
                + blankIfNull(backgroundRepeat);
        return fullBackground.equals("") ? "" : fullBackground.trim();
    }


    private String blankIfNull(String attribute) {
        return attribute == null ? "" : attribute + " ";
    }

}
